package dm;

/**
 * <p>Title: Domain Model Generator</p>
 *
 * <p>Description: Automatic generation of a domain model</p>
 *
 * <p>Copyright: Copyright (c) 2011</p>
 *
 * <p>Company: UTA</p>
 *
 * @author David Kung
 * @version 1.0
 */
import java.util.*;
public class DMG extends DMComp {
  DMComp nouns, transverbs, numerics, adjectives, adverb, verbPlusEd, verbPlusIng, partOf,
      tionNouns, syscapables, actorcapables, allOther, isa, verbnouns, reserveWords;
  public DMG() {
    super();
    syscapables=new DMComp("dict/syscapables.txt", "<SM");
    actorcapables=new DMComp("dict/actorcapables.txt", "<AM");
    nouns=new Nouns("dict/nouns.txt", "<N");
    verbPlusEd=new VerbPlusEd("dict/transverbs.txt", "<VED"); // must before transverbs
    verbPlusIng=new VerbPlusIng("dict/transverbs.txt", "<VING"); // must before transverb
    transverbs=new DMComp("dict/transverbs.txt", "<V");
    numerics=new DMComp("dict/numerics.txt", "<NUM");
    adjectives=new Adjectives("dict/adjectives.txt");
    adverb=new DMComp("dict/adverbs.txt", "<ADV");
    partOf=new DMComp("dict/partof.txt", "<AGG");
    //allOther=new AllOther();
    //isa=new DMComp("dict/isa.txt", "<ISA");
    verbnouns=new DMComp("dict/verbnouns.txt", "<VN");
    tionNouns=new TionNouns();
    //reserveWords=new ReserveWords("dict/reservewords.txt");
  }
    public String annotate(String desc) {
        Segment sgmt = new Segment(desc, false, null);
        ((TionNouns)tionNouns).collectTionWords(sgmt);
        tionNouns=new TionNouns("dict/tionwords.txt");
        //reserveWords.annotate(sgmt);
        tionNouns.annotate(sgmt);
        syscapables.annotate(sgmt);
        actorcapables.annotate(sgmt);
        numerics.annotate(sgmt);
        transverbs.annotate(sgmt);
        verbnouns.annotate(sgmt);
        nouns.annotate(sgmt);
        //isa.annotate(sgmt);
        adjectives.annotate(sgmt);
        adverb.annotate(sgmt);
        verbPlusEd.annotate(sgmt);
        verbPlusIng.annotate(sgmt);
        //partOf.annotate(sgmt);
        //allOther.annotate(sgmt);
        cleanUp(sgmt);
        //System.out.println("Clean up finished");
        Segment p = sgmt;
        String result = "";
        int charCount=0;
        while (p != null) {
          if (charCount+p.getContent().length()>70) {
            result += p.content + "\n";
            charCount=0;
          }
          else {
            result += p.content;
            charCount+=p.getContent().length();
          }
            p = p.next;
        }
        return result.trim();
      }
    public void cleanUp(Segment s) {
      Segment p = s, q=null;
      if (p != null ) q=p.next; else return;
      while (q != null) {
        if (q.content.trim().length() == 0) {
          p.next = q.next;
        } else {
          p=q;
        }
          q = q.next;
      }
      p=s; q=null;
      while (p != null) {
        if (p.content.indexOf("<N ")>=0) {
          if (q == null ) {
            q=p;
          } else {
            if (q.content.indexOf("<N ")>=0)
               q.content=q.content.replaceAll("<N ", "<NS ");
            q.content=q.content.replaceAll(">", "")+p.content.replaceAll("<N ", " ");
            q.next=p.next;
          }
        } else q=null;
        p=p.next;
      }
    }
    public void cleanUpbak(Segment s) {
      Segment p = s, q = null, r = null;
      int state = 0;
      String content = "";
      while (p != null) {
        if (p.skip = true) {
          switch (state) {
            case 0:
              if (p.content.indexOf("<N ") >= 0) {
                q = p;
                content += p.getContent().replaceAll("<N", "");
                state = 1;
                break;
              }
              if (p.content.indexOf("<ADJ ") >= 0) {
                q = p;
                content += p.getContent().replaceAll("<ADJ", "");
                state = 2;
                break;
              }
              break;
            case 1:
              if (p.content.indexOf("<N ") >= 0) {
                content += p.getContent().replaceAll("<N", "");
                state = 3;
                break;
              }
              else {
                content = "";
                state = 0;
                q = null;
              }
              break;
            case 2:
              if (p.content.indexOf("<N ") >= 0) {
                content += p.content.replaceAll("<N", "");
                state = 3;
                break;
              }
              if (p.content.indexOf("<ADJ") >= 0) {
                content += p.content.replaceAll("<ADJ", "");
                break;
              }
              content = "";
              q = null;
              state = 0;
              break;
            case 3:
              if (p.content.indexOf("<N") >= 0) {
                content += p.content.replaceAll("<N", "");
                break;
              }
              if (q != null) {
                q.content = "<NP " + content.replace('>', ' ') + ">";
                q.skip = true;
                q.next = p;
                q=null; content="";
                state = 0;
              }
              break;
          }
        } else {
          if (p.content.trim().length()>0) {
            if (state==3 && q != null) {
                q.content = "<NP " + content.replace('>', ' ') + ">";
                q.skip = true;
                q.next = p;
            }
            content=""; q=null; state=0;
          }
        }
        if (p != null) p = p.next;
      }
    }
  public static void main(String[] args) {
    DMG dmg=new DMG();
    String[] input=new String[1];
    input=util.PersistenceMgr.load(input, args[0]);
    String texts=" ";
    for(int i=0; i<input.length; i++) texts += input[i]+" ";
    String result=dmg.annotate(texts);
    util.PersistenceMgr.save(new String[] {result}, "annotated.txt");
    //System.out.println(result);
  }
}
